# Python中的日期类型引入
from datetime import timedelta
from datetime import datetime
# 引入Flask
from flask import Flask
# 引入扩展模块
from App.ext import init_ext
# 引入配置模块
from App.settings import envs
# from App.myconfig import envs
# 引入蓝图模块，有多少引入多少
from App.views.account import accountblue
# from App.models import accountblue
from App.views.student import studentblue
from App.views.dorm import dormblue
from App.views.accommodation import accommodationblue
from App.views.water_pay import waterpayblue
from App.views.electricity_pay import electricitypayblue
from App.views.breaks import breakblue
from App.views.alarm import alarmblue
from App.views.weather import weatherblue
from App.views.page_jump import pagejumpblue

# from App.views.room import roomblue
# from App.views.pet import petblue
# from App.views.plant import plantblue
# from App.views.order import orderblue


def create_app(env):
    # 创建app,由主入口manage.py进行调用create_app
    app = Flask(__name__)

    # 通过setting.py初始化此app
    # app.config.from_object(envs.get(env))
    # app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@127.0.0.1:3306/housebrainlite'
    # app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = 'False'
    app.config.from_object(envs.get(env))

    # 注册蓝图
    app.register_blueprint(accountblue)
    app.register_blueprint(studentblue)
    app.register_blueprint(dormblue)
    app.register_blueprint(accommodationblue)
    app.register_blueprint(waterpayblue)
    app.register_blueprint(electricitypayblue)
    app.register_blueprint(breakblue)
    app.register_blueprint(alarmblue)
    app.register_blueprint(weatherblue)
    app.register_blueprint(pagejumpblue)
    # 初始化第三方扩展库，包括SQLAlchemy及Migrate等第三方库
    init_ext(app=app)

    # 配置Flask-WTF，即设置session，通过加密或签名以不同的方式提升安全性
    app.config['SECRET_KEY'] = 'housebrain will change the world'
    # 设置session的保存时间
    app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=30)
    app.config['JSON_AS_ASCII'] = False
    # 设置缓存保存时间防止一直修改无效
    app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(seconds=1)

    return app
